home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / TNSERV.C < prev    next >
C/C++ Source or Header  |  1990-02-06  |  3KB  |  116 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "timer.h"
  5. #include "icmp.h"
  6. #include "netuser.h"
  7. #include "tcp.h"
  8. #include "telnet.h"
  9. #include "session.h"
  10.  
  11. struct tcb *tnet_tcb;
  12. tn1(argc,argv)
  13. char *argv[];
  14. {
  15.     struct socket lsocket;
  16.     extern int32 ip_addr;
  17.     void tnet_state();
  18.     void t_state(),rcv_char();
  19.     char tos = 0;
  20.  
  21.     /* Incoming Telnet */
  22.     lsocket.address = ip_addr;
  23.     if(argc < 2)
  24.         lsocket.port = TELNET_PORT;
  25.     else {
  26.         if ((lsocket.port = atoi(argv[1])) == 0)
  27.             lsocket.port = TELNET_PORT;
  28.         tos = get_tos(argv[2]);
  29.     }
  30.     tnet_tcb = open_tcp(&lsocket,NULLSOCK,TCP_SERVER,0,rcv_char,NULLVFP,tnet_state,tos,(char *)NULL);
  31.     return 0;
  32. }
  33. /* Handle incoming Telnet connect requests by creating a Telnet session,
  34.  * then change upcall vector so it behaves like an ordinary Telnet session.
  35.  *
  36.  */
  37. static void
  38. tnet_state(tcb,old,new)
  39. struct tcb *tcb;
  40. char old,new;
  41. {
  42.     struct telnet *tn;
  43.     struct session *s,*newsession();
  44.     long tloc;
  45.     void t_state(),tn_tx();
  46.     char *a;
  47.  
  48.     switch(new){
  49.     case ESTABLISHED:
  50.         log_tcp(tcb,"open Telnet");
  51.         time(&tloc);
  52.         /* Allocate a session descriptor */
  53.         if((s = newsession()) == NULLSESSION){
  54.             printf("\007%.15s Incoming Telnet call from %s refused; too many sessions\n",
  55.              ctime(&tloc) + 4,phsocket(&tcb->conn.remote));
  56.             fflush(stdout);
  57.             sndmsg(tcb,"Call rejected; too many sessions on remote system\n");
  58.             close_tcp(tcb);
  59.             return;
  60.         }
  61.         a = inet_n2h(tcb->conn.remote.address);
  62.         if((s->name = malloc((unsigned)strlen(a)+1)) != NULLCHAR)
  63.             strcpy(s->name,a);
  64.         s->type = TELNET;
  65.         s->parse = send_tel;
  66.         /* Create and initialize a Telnet protocol descriptor */
  67.         if((tn = (struct telnet *)calloc(1,sizeof(struct telnet))) == NULLTN){
  68.             printf("\007%.15s Incoming Telnet call refused; no space\n",
  69.                 ctime(&tloc) + 4);
  70.             fflush(stdout);
  71.             sndmsg(tcb,"Call rejected; no space on remote system\n");
  72.             close_tcp(tcb);
  73.             s->type = FREE;
  74.             return;
  75.         }
  76.         tn->session = s;    /* Upward pointer */
  77.         tn->state = TS_DATA;
  78.         s->cb.telnet = tn;    /* Downward pointer */
  79.  
  80.         tcb->user = (char *)tn; /* Upward pointer */
  81.         tn->tcb = tcb;        /* Downward pointer */
  82.         printf("\007%.15s Incoming Telnet session %lu from %s\n",
  83.          ctime(&tloc) + 4,(long)(s - sessions),phsocket(&tcb->conn.remote));
  84.         fflush(stdout);
  85.         tcb->s_upcall = t_state;
  86.         tcb->t_upcall = tn_tx;
  87.         return;
  88.     case CLOSED:
  89.         /* This will only happen if the connection closed before
  90.          * the session was set up, e.g., if we refused it because
  91.          * there were too many sessions, or if the server is being
  92.          * shut down.
  93.          */
  94.         if(tcb == tnet_tcb)
  95.             tnet_tcb = NULLTCB;
  96.         del_tcp(tcb);
  97.         break;
  98.     }
  99. }
  100. tn0()
  101. {
  102.     if(tnet_tcb != NULLTCB)
  103.         close_tcp(tnet_tcb);
  104.     return 0;
  105. }
  106. static
  107. sndmsg(tcb,msg)
  108. struct tcb *tcb;
  109. char *msg;
  110. {
  111.     struct mbuf *bp;
  112.  
  113.     bp = qstring(msg);
  114.     send_tcp(tcb,bp);
  115. }
  116.